var app = require('../../utils/util.js');
import {
	phone ,password
} from '../../utils/request/apiurl'
import api from '../../utils/request/api'


Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		processData: [{
				name: '验证手机号',
				start: '../../asset/images/now01@2x.png',
				end: '../../asset/images/yellow01@2x.png',
				icon: '../../asset/images/now01@2x.png'
			},
			{
				name: '绑定新密码',
				start: '../../asset/images/gray02@2x.png',
				end: '../../asset/images/yellow02@2x.png',
				icon: '../../asset/images/gray02@2x.png'
			},
			{
				name: '更换成功',
				start: '../../asset/images/gray03@2x.png',
				end: '../../asset/images/yellow03@2x.png',
				icon: '../../asset/images/gray03@2x.png'
			}
		],
		processData2: [{
				name: '验证手机号绑定密码',
				start: '../../asset/images/now01@2x.png',
				end: '../../asset/images/yellow01@2x.png',
				icon: '../../asset/images/now01@2x.png'
			},
			{
				name: '绑定成功',
				start: '../../asset/images/now02@2x.png',
				end: '../../asset/images/yellow02@2x.png',
				icon: '../../asset/images/gray02@2x.png'
			}
		],
		phone: '', //手机号
		phoneCode: '', //验证码
		// iscode: '', //用于存放验证码接口里获取到的code
		codename: '获取验证码',
		uid: '',
		isPayPassword: '',
		password: '', //支付密码
		ispassword: '', //再次输入支付密码
    numCode:60,//倒计时60s
	},
	//获取input输入框的值
	// 请输入手机号
	getPhoneValue: function(e) {
		this.setData({
			phone: e.detail.value
		})
	},
	// 请输入验证码
	getCodeValue: function(e) {
		// console.log(e)
		this.setData({
			phoneCode: e.detail.value
		})
	},
	getCodeValue1: function(e) {
		// console.log(e)
		this.setData({
			password: e.detail.value
		})
	},
	getCodeValue2: function(e) {
		// console.log(e)
		this.setData({
			ispassword: e.detail.value
		})
	},
	getCode: function() {
		var a = this.data.phone;
    var _this = this;
    if (_this.data.numCode < 60 && _this.data.numCode > 0) {
      return wx.showToast({
        title: '请稍后发送验证码',
        icon: 'none'
      })
    }
		// var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
		var myreg=/^1(3|4|5|6|7|8|9)\d{9}$/;
		if (this.data.phone == "") {
			wx.showToast({
				title: '手机号不能为空',
				icon: 'none',
				duration: 2000
			})
			return false;
		} else if (!myreg.test(this.data.phone)) {
			wx.showToast({
				title: '请输入正确的手机号',
				icon: 'none',
				duration: 2000
			})
			return false;
		} else {
			var source = {
				uid: this.data.uid,
				phone: this.data.phone,
			}
			// console.log(source)
			api.post(phone.createCode, source).then(res => {
				// console.log(res)
				// this.setData({
				// 	iscode:res,
				// })
				wx.showToast({
					title: '发送成功',
					icon: 'none',
					duration: 2000
				})
				// var num = 61;
				var timer = setInterval(function() {
          // num--;
          _this.setData({
            numCode: _this.data.numCode - 1,
          })
          if (_this.data.numCode <= 0) {
						clearInterval(timer);
						_this.setData({
							codename: '重新发送',
							disabled: false,
              numCode:60,
						})

					} else {
						_this.setData({
              codename: _this.data.numCode + "s"
						})
					}
				}, 1000)

			}).catch(err => {
				// console.log(err)
        wx.showToast({
          title: err.message,
          icon: 'none'
        })
			})

		}
	},
	//获取验证码
	getVerificationCode() {
		this.getCode();
		var _this = this
		_this.setData({
			disabled: true
		})
	},
	//提交表单信息
	save: function() {
		var source = {
			uid: this.data.uid,
			code: this.data.phoneCode,
		}
		// console.log(this.data.password)
		// console.log(this.data.ispassword)
		api.post(phone.verifyCode, source).then(res => {
			// debugger
			if (res == '验证码正确') {
				if (this.data.isPayPassword == 1) {
					wx.redirectTo({
						url: '../change-newPassword/change-newPassword',
					}) //页面跳转
				} else {
					var myreg = /^\d{6}$/;
					if (this.data.password == "" && this.data.ispassword == '') {
						wx.showToast({
							title: '密码不能为空',
							icon: 'none',
							duration: 2000
						})
						return false;
					} else if (!myreg.test(this.data.password) && !myreg.test(this.data.ispassword)) {
						wx.showToast({
							title: '请输入正确的密码',
							icon: 'none',
							duration: 2000
						})
						return false;
					} else if (this.data.password != this.data.ispassword) {
						wx.showToast({
							title: '两次密码输入不一样',
							icon: 'none',
							duration: 2000
						})
						return false;
					} else {
						var source = {
							uid: this.data.uid,
							password: this.data.password,
						}
						// console.log(source)
						api.post(password.PayPassword, source).then(res => {
							// console.log(res)
							// debugger
							if (res=='OK') {
								wx.showToast({
									title: '绑定成功',
									icon: 'none',
									duration: 2000
								})
								var userInfo=wx.getStorageSync('userInfo')
								userInfo.isPayPassword =1;
								wx.setStorageSync('userInfo', userInfo);
								wx.switchTab({
									url: '../personal-center/personal-center',
								}) //页面跳转
								// console.log(wx.getStorageSync('userInfo'),'userInfouserInfo')
							}
							// else{
							// 	wx.showToast({
							// 		title: '绑定失败',
							// 		icon: 'none'
							// 	})
							// 	wx.navigateTo({
							// 		url: '../personalInfo/personalInfo',
							// 	}) //页面跳转
							// }
						}).catch(err => {
							// console.log(err)
							wx.showToast({
								title: err.message,
								icon: 'none'
							})
						})
					}
				} 
			}else {
        wx.showToast({
          title: err.message,
          icon: 'none'
        })
			}
		})
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		// console.log(options)
		var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
		if (myreg.test(options.phone)) {
			this.setData({
				phone: options.phone
			})
		}
		if (options.isPayPassword) {
			this.setData({
				isPayPassword: options.isPayPassword
			})
		}
		var userInfo = wx.getStorageSync('userInfo');
		if (userInfo.rank) {
			this.setData({
				uid: userInfo.uid,
			})
		}
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {
		wx.showNavigationBarLoading() //在标题栏中显示加载
		this.onShow()
		setTimeout(function() {
			wx.hideNavigationBarLoading() //完成停止加载
			wx.stopPullDownRefresh() //停止下拉刷新
		}, 1500);
	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	//进度条的状态
	setPeocessIcon: function() {
		var index = 0 //记录状态为1的最后的位置
		var processArr = this.data.processData
		// console.log("progress", this.data.detailData.progress)
		for (var i = 0; i < this.data.detailData.progress.length; i++) {
			var item = this.data.detailData.progress[i]
			processArr[i].name = item.word
			if (item.state == 1) {
				index = i
				processArr[i].icon = "../../img/process_3.png"
				processArr[i].start = "#45B2FE"
				processArr[i].end = "#45B2FE"
			} else {
				processArr[i].icon = "../../img/process_1.png"
				processArr[i].start = "#EFF3F6"
				processArr[i].end = "#EFF3F6"
			}
		}
		processArr[index].icon = "../../img/process_2.png"
		processArr[index].end = "#EFF3F6"
		processArr[0].start = "#fff"
		processArr[this.data.detailData.progress.length - 1].end = "#fff"
		this.setData({
			processData: processArr
		})
	},
})
